package org.springcloud.plugin.framework.adapter;

import com.netflix.loadbalancer.Server;
import org.apache.commons.lang3.StringUtils;
import org.springcloud.plugin.common.constant.DiscoveryConstant;
import org.springcloud.plugin.common.entity.RuleEntity;
import org.springcloud.plugin.framework.cache.RuleCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.serviceregistry.Registration;

import java.util.Map;

/**
 * author: yanggld
 * date: 2019/12/14 12:46
 * version: 1.0
 * description: AbstractPluginAdapter
 */
public abstract class AbstractCoreAdapter implements CoreAdapter {

    @Autowired
    protected Registration registration;

    @Autowired
    protected RuleCache ruleCache;

    /**
     * 返回1
     * @return
     */
    @Override
    public String getServiceId() {
        return registration.getServiceId().toLowerCase();
    }

    @Override
    public String getGroup() {
        String group = getMetadata().get(DiscoveryConstant.GROUP);
        if (StringUtils.isEmpty(group)) {
            group = DiscoveryConstant.DEFAULT_GROUP;
        }
        return group;
    }

    @Override
    public String getHost() {
        return registration.getHost();
    }

    @Override
    public int getPort() {
        return registration.getPort();
    }

    @Override
    public Map<String, String> getMetadata() {
        return registration.getMetadata();
    }

    @Override
    public String getVersion() {
        String version = getMetadata().get(DiscoveryConstant.VERSION);
        if (StringUtils.isEmpty(version)) {
            version = DiscoveryConstant.DEFAULT;
        }
        return version;
    }

    @Override
    public String getRegion() {
        String region = getMetadata().get(DiscoveryConstant.REGION);
        if (StringUtils.isEmpty(region)) {
            region = DiscoveryConstant.DEFAULT;
        }
        return region;
    }

    @Override
    public String getEnvironment() {
        String env = getMetadata().get(DiscoveryConstant.ENVIRONMENT);
        if (StringUtils.isEmpty(env)) {
            env = DiscoveryConstant.DEFAULT;
        }
        return env;
    }

    @Override
    public String getServerVersion(Server server) {
        Map<String, String> serverMetadata = getServerMetadata(server);
        if (serverMetadata != null) {
            String  version = serverMetadata.get(DiscoveryConstant.VERSION);
            if (StringUtils.isNotEmpty(version)) {
                return version;
            }
        }
        return DiscoveryConstant.DEFAULT;
    }

    @Override
    public String getServerRegion(Server server) {
        Map<String, String> serverMetadata = getServerMetadata(server);
        if (serverMetadata != null) {
            String region = serverMetadata.get(DiscoveryConstant.REGION);
            if (StringUtils.isNotEmpty(region)) {
                return region;
            }
        }
        return DiscoveryConstant.DEFAULT;
    }

    @Override
    public String getServerEnvironment(Server server) {
        Map<String, String> serverMetadata = getServerMetadata(server);
        if (serverMetadata != null) {
            String env = serverMetadata.get(DiscoveryConstant.ENVIRONMENT);
            if (StringUtils.isNotEmpty(env)) {
                return env;
            }
        }
        return DiscoveryConstant.DEFAULT;
    }

    @Override
    public String getServerServiceId(Server server) {
        String serviceId = getServerMetadata(server).get(DiscoveryConstant.SPRING_APPLICATION_NAME);
        if (StringUtils.isEmpty(serviceId)) {
            serviceId = server.getMetaInfo().getAppName();
        }
        if (StringUtils.isEmpty(serviceId)) {
            throw new RuntimeException("Server ServiceId is null");
        }
        return serviceId.toLowerCase();
    }

    @Override
    public void setRule(RuleEntity ruleEntity) {
        ruleCache.put(DiscoveryConstant.RULE, ruleEntity);
    }

    @Override
    public RuleEntity getRule() {
        return ruleCache.get(DiscoveryConstant.RULE);
    }
}